package interview.offer;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author fangbin
 * @desc
 * @date 2020/5/8 6:29
 */
public class ReflectSample {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class rc = Class.forName("interview.offer.Robot");
        Robot r = (Robot) rc.newInstance();
        System.out.println("Class name is "+rc.getName());
        r.sayHi("bob");

        Method getHello = rc.getDeclaredMethod("throwHello",String.class);
        getHello.setAccessible(true);
        Object str = getHello.invoke(r,"BOB");
        System.out.println("getHello result is :"+str);

        Method sayHi = rc.getMethod("sayHi",String.class);
        sayHi.setAccessible(true);
        Field name = rc.getDeclaredField("name");
        name.set(r,"Alice");
        Object str2 = sayHi.invoke(r,"Bingo");
        System.out.println("sayHi result is :"+str2);

        System.out.println(System.getProperty("java.ext.dirs"));
        System.out.println(System.getProperty("java.class.path"));

    }
}
